package com.android.videotest.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.android.videotest.R;
import com.sansecy.android.simplevideo.SimpleVideoView;

import java.io.File;
import java.io.Serializable;

public class VideoViewActivity2 extends AppCompatActivity {

    private static final String TAG = "VideoViewActivity2";
    public static final String URI = "uri";
    public static final String FILE = "file";
    public static final String URL = "url";

    public static void start(Context context, File file) {
        Intent starter = new Intent(context, VideoViewActivity2.class);
        starter.putExtra(FILE, file);
        context.startActivity(starter);
    }

    public static void start(Context context, String url) {
        Intent starter = new Intent(context, VideoViewActivity2.class);
        starter.putExtra(URL, url);
        context.startActivity(starter);
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, VideoViewActivity2.class);
        context.startActivity(starter);
    }

    public static void start(Context context, Uri uri) {
        Intent starter = new Intent(context, VideoViewActivity2.class);
        starter.putExtra(URI, uri);
        context.startActivity(starter);
    }

    SimpleVideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_view2);
        videoView = findViewById(R.id.video_View);

        Serializable file = getIntent().getSerializableExtra(FILE);
        if (file != null) {
            Uri uri = Uri.fromFile((File) file);
            videoView.loadUri(uri);
        }
        Parcelable uri = getIntent().getParcelableExtra(URI);
        if (uri != null) {
            videoView.loadUri((Uri) uri);
        }

        Serializable url = getIntent().getSerializableExtra(URL);
        if (url != null) {
            videoView.loadUrl((String) url);
        }
        videoView.openVideo();
//        videoView.getPlayer().setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//            @Override
//            public void onPrepared(MediaPlayer mp) {
//                Log.d(this.getClass().getName(), "onPrepared:getCurrentPosition " + mp.getCurrentPosition());
//            }
//        });
////        videoView.start();
//        VideoController videoController = new VideoController(this);
//        videoController.attachToVideoView(videoView);
    }

    @Override
    protected void onStart() {
        super.onStart();
        videoView.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        videoView.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        videoView.release();
        videoView.stopPlayback();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "onRequestPermissionsResult: 已授予权限");
                } else {
                    Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "onRequestPermissionsResult: 禁止权限");
                }
                break;
            default:
        }
    }
}
